/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ruipengkj.bean.TProductCatalog;
import com.ruipengkj.commons.base.query.BaseQuery;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.dao.TProductCatalogDao;
import com.ruipengkj.service.TProductCatalogService;
/**
 * 说明：
 * 产品目录Service层接口实现
 * @Author：李智深
 * @Date：2012-03-16
 */
@Service
public class TProductCatalogServiceImpl implements TProductCatalogService{
	@Resource
	private TProductCatalogDao TProductCatalogDao;//DAO层
	//查询所有
	@Override
	public List<TProductCatalog> findService(TProductCatalog entity) throws Exception {
		String hql = "select o from TProductCatalog o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if(entity.getTProductCatalog()!=null&&!entity.getTProductCatalog().getCatalogID().equals("")){
				whereHQL.append("TProductCatalog=?");
				param.add(entity.getTProductCatalog());
			}
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("priority", BaseQuery.ASC);
		return TProductCatalogDao.findDao(hql, whereHQL.toString(), param, orderby);
	}
	
	//查询所有
	@Override
	public List<TProductCatalog> findListService(String ParentID) throws Exception {
		String hql = "select new TProductCatalog(o.catalogID, o.name, o.des) from TProductCatalog o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (ParentID == null||ParentID.equals("")) {
			whereHQL.append("o.TProductCatalog is null");
		}else{
			whereHQL.append("o.TProductCatalog=?");
			param.add(new TProductCatalog(ParentID));
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("priority", BaseQuery.ASC);
		return TProductCatalogDao.findDao(hql, whereHQL.toString(), param, orderby);
	}
	//查询所有
	@Override
	public List<TProductCatalog> findByNameService(String name) throws Exception {
		String hql = "select new TProductCatalog(o.catalogID) from TProductCatalog o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (name != null && !name.equals("")) {
			whereHQL.append("o.name=?");
			param.add(name);
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("priority", BaseQuery.ASC);
		return TProductCatalogDao.findDao(hql, whereHQL.toString(), param, orderby);
	}
	
	//根据ID翻页查询
	@Override
	public PageModel<TProductCatalog> getScrollService(int firstResult,
			int maxResult) throws Exception {
		String hql = "select new TProductCatalog(o.catalogID, o.name, o.des) from TProductCatalog o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("priority", BaseQuery.ASC);
		return TProductCatalogDao.getScrollDao(hql, whereHQL.toString(), param, firstResult, maxResult, orderby);
	}		
	//根据ID翻页查询
	@Override
	public PageModel<TProductCatalog> getScrollService(int firstResult,
			int maxResult, TProductCatalog entity) throws Exception {
		String hql = "select o from TProductCatalog o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if(whereHQL.length()>4)
				whereHQL.delete(whereHQL.length()-5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("priority", BaseQuery.ASC);
		return TProductCatalogDao.getScrollDao(hql, whereHQL.toString(), param, firstResult, maxResult, orderby);
	}
	//修改
	@Override
	public void modifyService(TProductCatalog entity) throws Exception {
		TProductCatalogDao.modifyDao(entity);
		
	}
	//添加
	@Override
	public void saveService(TProductCatalog entity) throws Exception {
		TProductCatalogDao.saveDao(entity);
	}

	public TProductCatalogDao getTProductCatalogDao() {
		return TProductCatalogDao;
	}

	public void setTProductCatalogDao(TProductCatalogDao TProductCatalogDao) {
		this.TProductCatalogDao = TProductCatalogDao;
	}
	//根据父节点ID查询子节点
	@Override
	public List<TProductCatalog> findChildByParentCatalogIDService(String ParentID)
			throws Exception {
		String hql = "select o from TProductCatalog o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (ParentID == null||ParentID.equals("")) {
			whereHQL.append("o.TProductCatalog is null");
		}else{
			whereHQL.append("o.TProductCatalog=?");
			param.add(new TProductCatalog(ParentID));
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("priority", BaseQuery.ASC);
		return TProductCatalogDao.findDao(hql, whereHQL.toString(), param, orderby);
	}
	//根据ID删除
	@Override
	public void deleteService(String id) throws Exception {
		TProductCatalogDao.deleteDao(id);
		
	}
	//根据ID查询
	@Override
	public TProductCatalog getByIdService(String id) throws Exception {
		return TProductCatalogDao.getByIdDao(id);
	}
	//添加或修改
	@Override
	public void saveOrUpdateService(TProductCatalog entity) throws Exception {
		TProductCatalogDao.saveOrUpdateDao(entity);
	}
	//添加返回ID
	@Override
	public Serializable saveRetService(TProductCatalog entity) throws Exception {
		return TProductCatalogDao.saveRetDao(entity);
	}

	@Override
	public List<TProductCatalog> findSalesService()
			throws Exception {
		String hql = "select new TProductCatalog(o.catalogID, o.name, o.des, o.priority) from TProductCatalog o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		
			whereHQL.append("o.TProductCatalog is null ").append(BaseQuery.AND);
			whereHQL.append("(o.intoSales is null ").append(BaseQuery.OR);
			whereHQL.append(" o.intoSales<>?)");
			param.add("仅进货");
		
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("priority", BaseQuery.ASC);
		return TProductCatalogDao.findDao(hql, whereHQL.toString(), param, orderby);
	}
}